<template>
  <el-tabs v-model="activeName">

    <el-tab-pane label="统一调价" name="first">
      <div class="app-container">
        <el-row :gutter="1">
          <!--用户数据-->
          <el-col :span="24" :xs="24">
            <el-table v-loading="loading" :data="interList">
              <el-table-column label="接口名称" align="center" prop="name" width="200"/>
              <el-table-column label="接口描述" align="center" prop="explain" width="400"/>
              <el-table-column label="类型" align="center" prop="type" width="100"/>
              <el-table-column label="单次原价" align="center" prop="originalPrice" width="100"/>
              <el-table-column label="定价" align="center" prop="unitPrice" width="100"/>
              <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
              <el-table-column
                label="操作"
                align="center"
                width="160"
                class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['system:user:edit']"
                  >修改
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"/>
          </el-col>
        </el-row>

        <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
          <el-form ref="form" :model="form" label-width="80px">
            <el-row>
              <el-col :span="24">
                <el-form-item label="定价">
                  <el-input v-model="form.unitPrice" placeholder="请输入新定价" maxlength="30"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="原价">
                  <el-input v-model="form.originalPrice" placeholder="请输入原价" maxlength="30"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="接口描述">
                  <el-input v-model="form.explain" placeholder="接口描述" type="textarea"
                            :autosize="{ minRows: 2, maxRows: 4}" maxlength="300"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>

      </div>
    </el-tab-pane>

    <el-tab-pane label="定制调价" name="second">
      <el-form :model="queryParamsjk" size="small" :inline="true" label-width="68px">
        <el-form-item label="用户名称">
          <el-input
            v-model="queryParamsjk.userName"
            placeholder="请输入用户名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input
            v-model="queryParamsjk.phonenumber"
            placeholder="请输入手机号码"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="userList">
        <el-table-column label="用户名称" align="center" prop="userName"/>
        <el-table-column label="用户昵称" align="center" prop="nickName"/>
        <el-table-column label="手机号码" align="center" prop="phonenumber"/>
        <el-table-column label="企业名称" align="center" prop="comName"/>
        <el-table-column label="企业负责人" align="center" prop="functionary"/>
        <el-table-column label="商户号" align="center" prop="merchantCode"/>
        <el-table-column label="余额" align="center" prop="balance"/>
        l<el-table-column label="接口价格" align="center" type="expand">
          <template slot-scope="scope">
            <el-row justify="center" style="margin-left: 30px">
              <template v-for="(item, index) in scope.row.ins">
                <el-col :span="6">
                  <div class="grid-content bg-purple">
                    <p>
                      <i class="el-icon-money"></i>{{ item.name }}：<a>{{ item.price }}元
                      {{ item.id != null ? '(定制价)' : '' }}</a>
                      <el-button style="margin-left: 10px"
                                 type="primary"
                                 size="mini"
                                 icon="el-icon-cloudy"
                                 plain
                                 @click="xgjg(scope.row, index)">修改
                      </el-button>
                    </p>
                  </div>
                </el-col>
              </template>
            </el-row>
          </template>
        </el-table-column>

      </el-table>

      <pagination
        v-show="totaljk>0"
        :total="totaljk"
        :page.sync="queryParamsjk.pageNum"
        :limit.sync="queryParamsjk.pageSize"
        :pageSizes="[30, 50]"
        @pagination="getlistUser"
      />
    </el-tab-pane>

    <el-dialog :title="titlexgjg" :visible.sync="isxggg" width="400px">
      <el-form :model="jgform" label-width="100px">
        <el-row>
          <el-col :span="6">
            <el-form-item :label="xgjglabel">
              <el-input
                v-model="jgform.jg"
                :placeholder="xgjglabel"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="qdxgjg">确 定</el-button>
        <el-button @click="isxggg=!isxggg">取 消</el-button>
      </div>
    </el-dialog>
  </el-tabs>
</template>

<script>
  import {getSMAndInter, listJieKou, updateJieKou, updateJieKoujg} from "@/api/jiekou/jiekou";

  export default {
    data() {
      return {
        activeName: 'first',
        userList: null,
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 总条数
        totaljk: 0,
        // 表格数据
        interList: null,
        // 弹出层标题
        title: "",
        titlexgjg: '',
        jgform: {
          jg: null
        },
        // 是否显示弹出层
        xgjglabel: null,
        open: false,
        isxggg: false,
        xz: null,
        index: null,
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null
        },
        // 查询参数
        queryParamsjk: {
          pageNum: 1,
          pageSize: 20,
          userName: null,
          phonenumber: null
        },
      };
    },

    watch: {},
    created() {
      this.getList();
      this.getlistUser()
    },
    methods: {
      /** 查询用户列表 */
      getList() {
        this.loading = true;
        listJieKou(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.interList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },

      getlistUser() {
        getSMAndInter(this.addDateRange(this.queryParamsjk, this.dateRange)).then(response => {
            this.userList = response.rows;
            this.totaljk = response.total;
            this.loading = false;
          }
        )
      },

      xgjg(row, index) {
        this.index = index
        this.xz = row
        this.titlexgjg = '修改' + row.merchantCode + '对接口 ' + row.ins[index].name + '的调用价格'
        this.jgform.jg = row.ins[index].price
        this.xgjglabel = row.ins[index].name
        this.isxggg = true
      },

      qdxgjg() {
        var jkdj = this.xz.ins[this.index]
        jkdj.price = this.jgform.jg
        jkdj.userId = this.xz.userId
        jkdj.merchantCode = this.xz.merchantCode
        updateJieKoujg(jkdj).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.isxggg = false;
            this.getlistUser();
          }
        );
      },

      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      resetQuery() {
        this.queryParamsjk.userName = null,
        this.queryParamsjk.phonenumber = null
      },

      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },

      // 表单重置
      reset() {
        this.form = {
          jfCode: null,
          explain: null,
          id: null,
          originalPrice: null,
          unitPrice: null
        }
        this.resetForm("form")
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.title = row.name + "调用单次价格",
          this.reset()
        this.open = true
        this.form.jfCode = row.jfCode
        this.form.unitPrice = row.unitPrice
        this.form.originalPrice = row.originalPrice
      },

      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateJieKou(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false
              this.getList()
            })
          }
        })
      },
    }
  };
</script>
